<?php

	eb_include_class('EbTableSQLModel');
	
	class BoutiqueModel extends ObjectModel
	{
		public function __construct()
		{
			parent::init('vp_boutique','VarPeople/BoutiqueVO','bo_id');
		}
				
		public function getColumns()
		{
			return array(
				new EbTableColumn("bo_id","int",true,EB_PRIMARY_KEY,true),
				new EbTableColumn("ca_id","int"),
				new EbTableColumn("ob_id","int"),
				new EbTableColumn("mediafolder_id","int"),
				new EbTableColumn("description","text"),
				new EbTableColumn("activity","text"),
				new EbTableColumn("contact","varchar(64)"),
				new EbTableColumn("adress","text"),
				new EbTableColumn("city","varchar(64)"),
				new EbTableColumn("lon","varchar(32)"),
				new EbTableColumn("lat","varchar(32)"),
				new EbTableColumn("hits","int")
			);
		}		
				
		public function selectListingByCategorie($ca_id)
		{
			return $this->selectListing('vp_categorie.ca_id = '.$ca_id);
		}
		
		public function selectListing($w = null)
		{			
			$sql = 'SELECT eb_object.name,eb_object.urlname,vp_boutique.*,vp_categorie.name AS categorie 
					FROM vp_boutique 
					INNER JOIN eb_object ON eb_object.ob_id = vp_boutique.ob_id
					INNER JOIN vp_categorie ON vp_categorie.ca_id = vp_boutique.ca_id';

			if (!is_null($w))
			{
				$sql .= ' WHERE '.$w;
			}
		
			$items = $this->execute($sql);

			if (count($items) > 0)
			{
				// Get Boutiques Media Folder
				$boutique_media_folder = EbModel::getModel('Media','MediaFolder')->getFolderPath(2);
				
				$buffer = '';
				
				foreach ($items AS $item)
				{
					$buffer .= $item->mediafolder_id.',';
					$item->urlthumbnail = '';
				}
				
				$buffer = substr($buffer,0,strlen($buffer)-1);
				
				$sql = 'SELECT eb_object.name,md_media.fd_id,md_item.code 
						FROM md_item
						INNER JOIN md_media ON md_media.md_id = md_item.md_id
						INNER JOIN md_folder ON md_folder.fd_id = md_media.fd_id
						INNER JOIN eb_object ON eb_object.ob_id = md_folder.ob_id
						WHERE md_item.type = 2 AND md_media.fd_id IN ('.$buffer.')';
							
				$media_items = $this->execute($sql);
				
				$media_provider = new Media_HttpProvider();
							
				foreach ($items AS $item)
				{
					$item->url = '/'.$item->urlname;
					
					foreach ($media_items AS $media)
					{
						if ($media->fd_id == $item->mediafolder_id)
						{
							$media_provider->folder = $boutique_media_folder.$media->name.'/';
							
							$item->urlthumbnail = $media_provider->getFileUrl($media->code);
						}
					}
				}
			}
			
			return $items;
		}
		
		public function selectBySearch($tags)
		{
			$tags=strtolower($tags);                
			$mots = str_replace("+", " ", trim($tags));  
			$mots = str_replace("\"", " ", $mots); 	          
			$mots = str_replace(",", " ", $mots); 	         
			$mots = str_replace(":", " ", $mots); 	 

			$mots = explode(" " , $mots);
			$buffer = '(';			
			
			foreach ($mots AS $mot)
			{
				$buffer .= 'LOWER(description) LIKE "%'.$mot.'%" AND ';
			}
			
			$buffer = substr($buffer,0,$buffer - 4);
			
			$buffer .= ') OR (';
			
			foreach ($mots AS $mot)
			{
				$buffer .= 'LOWER(eb_object.name) LIKE "%'.$mot.'%" AND ';
			}
			
			$buffer = substr($buffer,0,$buffer - 4);			
			
			return $this->selectListing($buffer.')');
		}
		
		public function selectProfileFromUrl($name)
		{
			$sql = 'SELECT eb_object.*,vp_boutique.*,vp_categorie.name AS categorie,vp_categorie.urlname AS urlcategorie ';
			$sql .= 'FROM vp_boutique ';
			$sql .= 'INNER JOIN eb_object ON eb_object.ob_id = vp_boutique.ob_id ';
			$sql .= 'LEFT OUTER JOIN vp_categorie ON vp_categorie.ca_id = vp_boutique.ca_id ';
			$sql .= 'WHERE eb_object.urlname = \''.$name.'\' LIMIT 1';
			
			$buffer = $this->execute($sql);
			
			return count($buffer) == 1 ? $buffer[0] : null;
		}
	}